package com.frxuexi.sign.view

import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.TimeUtils
import com.frxuexi.lib_base.entity.SignBeanItem
import com.frxuexi.lib_base.mvvm.BaseActivity
import com.frxuexi.lib_base.net.Const
import com.frxuexi.sign.SignViewModel
import com.frxuexi.sign.databinding.ActivitySignBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.Timer
import kotlin.concurrent.timerTask

@Route(path = "/sign/SignActivity")
@AndroidEntryPoint
class SignActivity : BaseActivity<ActivitySignBinding, SignViewModel>() {
    var timer:Timer?=null
    val spUtils = SPUtils.getInstance()
    override fun initData() {
        vm.signData.observe(this){
//            ToastUtils.showLong("打卡成功")
            if(it.morningCheckIn.isNullOrEmpty()){
                spUtils.put("dk","sout,${vm.getDate(System.currentTimeMillis())}")
                binding.btnSign.isEnabled=false
            }else{
                spUtils.put("dk","sign,${vm.getDate(System.currentTimeMillis())}")
                binding.signTvdaka.setText("下午打卡")
                binding.signTvtime.setText("18:00-24:00")
            }
        }
    }

    override fun initView() {

        binding.btnSign.setOnClickListener {
            val millis = System.currentTimeMillis()
            var status="正常"
            var morning=""
            var afternoon=""
            if(binding.signTvdaka.text.toString()=="上午打卡"){
                morning=vm.getDateTime(millis)
                if(morning.split(" ")[1]>"09:00:00"){
                    status="迟到"
                }
            }else{
                afternoon=vm.getDateTime(millis)
                if(afternoon.split(" ")[1]<"18:00:00"){
                    status="早退"
                }
            }
            val item = SignBeanItem(
                afternoon,
                spUtils.getString("lat", ""),
                null,
                "八维学院",
                morning,
                status,
                spUtils.getInt(Const.UID)
            )
            vm.sign(item)
        }

        binding.conLocation.setOnClickListener {
            ARouter.getInstance().build("/sign/MapActivity").navigation()
        }

        lifecycleScope.launch(Dispatchers.IO) {
            timer= Timer()
            timer?.schedule(timerTask {
                val time = vm.getTime(System.currentTimeMillis())
                lifecycleScope.launch(Dispatchers.Main) {
                    binding.signTime.setText("${time}")
                }
            },0,1000)
        }

        val dateFont = vm.getDateFont(System.currentTimeMillis())
        val week = TimeUtils.getChineseWeek(System.currentTimeMillis()).replace("周","星期")
        binding.signDate.setText("${dateFont} ${week}")

//        存储格式：sign/sout,2025-06-07
        val info = spUtils.getString("dk", "")
        if(info.isNotEmpty()){
            val split = info.split(",")
            if(split[1]==vm.getDate(System.currentTimeMillis())){//今天打过卡
                if(split[0]=="sign"){//今天已经打了上班卡
                    binding.signTvdaka.setText("下午打卡")
                    binding.signTvtime.setText("18:00-24:00")
                }else{//说明今天已经打了下班卡
                    binding.btnSign.isEnabled=false
                }
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if(timer!=null){
            timer?.cancel()
            timer==null
        }
    }

}